<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分库分表场景下的主键ID生成方案</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Tahoma', Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
            color: white;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            border-radius: 12px;
            overflow: hidden;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.15);
        }
        .pros-cons {
            border-left: 4px solid #6e8efb;
        }
        .feature-icon {
            font-size: 2rem;
            color: #6e8efb;
            margin-bottom: 1rem;
        }
        .section-title {
            position: relative;
            padding-bottom: 10px;
        }
        .section-title:after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 0;
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #6e8efb 0%, #a777e3 100%);
        }
        .comparison-table th {
            background-color: #f8f9fa;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">分库分表场景下的主键ID生成方案</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-3xl font-medium">探索分布式系统中高效、可靠的主键生成策略</p>
                <div class="w-full max-w-3xl bg-white bg-opacity-20 rounded-lg p-6 backdrop-blur-sm">
                    <p class="text-lg md:text-xl">在分库分表架构中，传统自增ID方式不再适用。我们需要采用分布式ID生成方案来确保全局唯一性、有序性和高性能。</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-6xl">
            <!-- Introduction -->
            <div class="mb-16">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 font-serif section-title">为什么需要分布式ID生成方案？</h2>
                <div class="grid md:grid-cols-2 gap-8">
                    <div class="bg-white p-6 rounded-lg shadow-md">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-database mr-3 text-blue-500"></i> 分库分表带来的挑战
                        </h3>
                        <p class="text-gray-700">在分布式数据库环境中，传统的自增ID会导致不同库表生成相同的ID，破坏数据唯一性。我们需要能够跨库表生成全局唯一ID的方案。</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-bullseye mr-3 text-purple-500"></i> 理想ID的特性
                        </h3>
                        <ul class="text-gray-700 space-y-2">
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 全局唯一性</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 高性能生成</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 有序或趋势递增</li>
                            <li><i class="fas fa-check-circle text-green-500 mr-2"></i> 高可用性</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Solutions -->
            <h2 class="text-2xl md:text-3xl font-bold mb-8 font-serif section-title">六种主流解决方案</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <!-- UUID -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-fingerprint"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">UUID</h3>
                        </div>
                        <p class="text-gray-700 mb-4">全局唯一标识符，通过算法生成，不依赖数据库。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>无需依赖数据库，完全在应用层生成</li>
                                <li>分布式场景中不同节点都可以独立生成</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>较长（36字符），占用更多存储空间</li>
                                <li>无序，索引和查询性能较差</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- 数据库序列 -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-sort-numeric-up-alt"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">数据库序列</h3>
                        </div>
                        <p class="text-gray-700 mb-4">使用数据库内建的序列生成机制，每个分库独立维护一组序列。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>简单易用，数据库本身支持</li>
                                <li>可以保证各分库内ID的有序性</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>需要设置不同的起始序列号或步长</li>
                                <li>高并发环境下可能成为瓶颈</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- Snowflake -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fab fa-twitter"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">Snowflake 算法</h3>
                        </div>
                        <p class="text-gray-700 mb-4">Twitter开发的分布式ID生成算法，生成64位的唯一ID。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>高效生成全局唯一的、有序的ID</li>
                                <li>可用于大规模分布式系统</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>需要依赖服务器时钟</li>
                                <li>需要部署生成器服务</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- 数据库分段号段 -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-layer-group"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">数据库分段号段</h3>
                        </div>
                        <p class="text-gray-700 mb-4">在每个数据库节点预先分配一段号段，如1-1000分配给库1。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>保证了各个库生成的ID不冲突且有序</li>
                                <li>生成效率高，减少频繁的ID查询</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>可能导致ID分配不均</li>
                                <li>号段管理和调整复杂</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- Redis/Zookeeper -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-server"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">Redis/Zookeeper</h3>
                        </div>
                        <p class="text-gray-700 mb-4">借助Redis或Zookeeper来生成唯一的ID。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>生成高效，支持高并发</li>
                                <li>支持分布式集群环境</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>需要额外部署和管理中间件</li>
                                <li>单点故障可能影响ID生成</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- 混合模式 -->
                <div class="card bg-white">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="feature-icon">
                                <i class="fas fa-random"></i>
                            </div>
                            <h3 class="text-xl font-bold ml-3">混合模式</h3>
                        </div>
                        <p class="text-gray-700 mb-4">结合多个策略生成ID，如Snowflake+业务ID。</p>
                        <div class="pros-cons pl-4 mb-4">
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-check text-green-500 mr-2"></i> 优点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1 mb-3">
                                <li>灵活性强，可自定义生成规则</li>
                                <li>确保ID唯一性且具备有序性</li>
                            </ul>
                            <h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-times text-red-500 mr-2"></i> 缺点</h4>
                            <ul class="list-disc list-inside text-gray-700 space-y-1">
                                <li>实现复杂度较高</li>
                                <li>需要考虑各种边界情况</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Comparison Chart -->
            <div class="mt-16 bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 font-serif section-title">方案比较</h2>
                <div class="overflow-x-auto">
                    <table class="min-w-full comparison-table">
                        <thead>
                            <tr class="bg-gray-100">
                                <th class="py-3 px-4 text-left">方案</th>
                                <th class="py-3 px-4 text-left">唯一性</th>
                                <th class="py-3 px-4 text-left">有序性</th>
                                <th class="py-3 px-4 text-left">性能</th>
                                <th class="py-3 px-4 text-left">复杂度</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            <tr>
                                <td class="py-3 px-4 font-medium">UUID</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4"><i class="fas fa-times text-red-500"></i></td>
                                <td class="py-3 px-4">中</td>
                                <td class="py-3 px-4">低</td>
                            </tr>
                            <tr>
                                <td class="py-3 px-4 font-medium">数据库序列</td>
                                <td class="py-3 px-4">分库内唯一</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4">中低</td>
                                <td class="py-3 px-4">中</td>
                            </tr>
                            <tr>
                                <td class="py-3 px-4 font-medium">Snowflake</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4">高</td>
                                <td class="py-3 px-4">中高</td>
                            </tr>
                            <tr>
                                <td class="py-3 px-4 font-medium">分段号段</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4">高</td>
                                <td class="py-3 px-4">高</td>
                            </tr>
                            <tr>
                                <td class="py-3 px-4 font-medium">Redis/Zookeeper</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4">可选</td>
                                <td class="py-3 px-4">中高</td>
                                <td class="py-3 px-4">高</td>
                            </tr>
                            <tr>
                                <td class="py-3 px-4 font-medium">混合模式</td>
                                <td class="py-3 px-4"><i class="fas fa-check text-green-500"></i></td>
                                <td class="py-3 px-4">可选</td>
                                <td class="py-3 px-4">取决于方案</td>
                                <td class="py-3 px-4">高</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Decision Flow -->
            <div class="mt-16 bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-2xl md:text-3xl font-bold mb-6 font-serif section-title">选择指南</h2>
                <div class="mermaid">
                    graph TD
                    A[需要分布式ID生成方案?] -->|是| B{是否需要高性能?}
                    B -->|是| C{是否需要有序ID?}
                    B -->|否| D[UUID]
                    C -->|是| E[Snowflake或分段号段]
                    C -->|否| F[Redis/Zookeeper]
                    A -->|否| G[数据库自增ID]
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-6xl px-4 text-center">
            <p class="text-lg mb-2">技术小馆</p>
            <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors duration-300">http://www.yuque.com/jtostring</a>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>